import { ref } from 'vue'
import {
	defineStore
} from 'pinia'

export const useSystemStore = defineStore('system', () => {
	/**
	 * 胶囊信息
	 */
	const capsule = ref<UniApp.GetMenuButtonBoundingClientRectRes>(null)
	/**
	 * 竖屏正方向下的安全区域插入位置
	 */
	const safeAreaInsets = ref<UniApp.SafeAreaInsets>(null)
	/**
	 * 状态栏高度
	 */
	const statusBarHeight = ref<number>(0)
	/**
	 * 高度
	 */
	const height = ref<number>(0)

	/**
	 * 异步获取系统信息
	 */
	const getSystemInfo = () => {
		uni.getSystemInfo({
			success: (e : UniApp.GetSystemInfoResult) => {
				safeAreaInsets.value = e.safeAreaInsets
				// 状态栏高度
				statusBarHeight.value = e.statusBarHeight
				// 获取右上角胶囊的信息
				capsule.value = uni.getMenuButtonBoundingClientRect()
				height.value = capsule.value.height + (capsule.value.top - statusBarHeight.value) * 2
			}
		})
	}

	return { capsule, safeAreaInsets, statusBarHeight, height, getSystemInfo }
}, {
	persist: {
		storage: {
			setItem(key, value) {
				uni.setStorageSync(key, value)
			},
			getItem(key) {
				return uni.getStorageSync(key)
			}
		}
	}
})